package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Menu;
import com.hzdl.car.bean.User;
import com.hzdl.car.service.MenuService;
import com.hzdl.car.util.Common;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.util.ResultObj;
import com.hzdl.car.util.TreeNode;
import com.hzdl.car.vo.MenuVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Resource
    private MenuService menuService;
    @RequestMapping("findAllMenu")
    public Map<String,Object> findAllMenu(HttpSession session){
        //设置查询条件
        MenuVo menuVo=new MenuVo();
        menuVo.setAvailable(1);
        List<Menu> menuList=new ArrayList<>();


        Object o=session.getAttribute("user");
        if(o!=null){
            User user=(User)o;
            if(user.getType()==1){
                menuList=menuService.findAllMenu(menuVo);
            }else {
                menuList=menuService.findAllMenuByUser(1,user.getUserId());
            }
        }



        List<TreeNode> treeNodes=new ArrayList<>();
        for(Menu m1:menuList){
            TreeNode treeNode = new TreeNode(m1.getId(),m1.getpId(),m1.getName(),m1.getIcon(),m1.getHref(),m1.getOpen()==1?true:false,m1.getTarget());
            treeNodes.add(treeNode);

        }

        List<TreeNode> treeNodes1=new ArrayList<>();

        for(TreeNode t1:treeNodes){
            if(t1.getpId()==1){
                treeNodes1.add(t1);
            }
            for(TreeNode t2:treeNodes){
                if(t1.getId()==t2.getpId()){
                    t1.getChildren().add(t2);
                }
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("contentManagement",treeNodes1);
        return  map;

    }
    @RequestMapping("menuLeftTreeJson")
    public HzdlResultJson menuLeftTreeJson(){
        MenuVo menuVo = new MenuVo();
        menuVo.setAvailable(1);
        // 查询数据库
        List<Menu> menuList =  menuService.findAllMenu(menuVo);

        List<TreeNode> treeNodes = new ArrayList<>();
        // 把menuList 转换成treeNodes 类型
        for(Menu m1: menuList){
            TreeNode treeNode = new TreeNode(m1.getId(),m1.getpId(),m1.getName(),m1.getIcon(),m1.getHref(),m1.getOpen()==1?true:false,m1.getTarget());
            treeNodes.add(treeNode);
        }
        // 所有的菜单数据都转换成了TreeNode

        List<TreeNode> treeNodes2 = new ArrayList<>();
        Integer id =0;
        Common.getTreeNodeMenu(treeNodes,id,treeNodes2);

        return HzdlResultJson.ok(treeNodes2);

    }
    @RequestMapping("findMenuRight")
    public HzdlResultJson menuRightTreeJson(MenuVo menuVo){
        PageInfo<Menu> pageInfo=menuService.findMenuBYWhere(menuVo);
        return  HzdlResultJson.ok(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("addMenu")
    public ResultObj addMenu(MenuVo menuVo){

        try{
            menuService.add(menuVo);
            return  ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 编辑
     * @param menuVo
     * @return
     */
    @RequestMapping("updateMenu")
    public ResultObj updateMenu(MenuVo menuVo){
        try{
            if(menuVo.getpId()==null){
                MenuVo menuVo1=menuService.findById(menuVo.getId());
                menuVo.setpId(menuVo1.getpId());
            }
            menuService.update(menuVo);
            return  ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            return ResultObj.UPDATE_ERROR;
        }
    }
    //判断是否存在子节点
    @RequestMapping("exitsChildren")
    public HzdlResultJson exitsChildren(MenuVo menuVo){
        try{
            int row = menuService.exitsChildren(menuVo.getId());
            return  HzdlResultJson.ok(row);
        }catch (Exception e){
            return HzdlResultJson.error(-1,e.getMessage());
        }
    }
    //删除菜单
    @RequestMapping("deleteMenu")
    public ResultObj deleteMenu(MenuVo menuVo){
        try{
            menuService.delById(menuVo.getId());
            return  ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }
    @RequestMapping("findMenuRightById")
    public HzdlResultJson findMenuRightById(MenuVo menuVo){
        PageInfo<Menu>  pageInfo=menuService.findMenuById(menuVo);
        return HzdlResultJson.ok(pageInfo.getTotal(),pageInfo.getList());
    }


}
